/*jshint esversion: 6 */
/**
 * 获取广告ad
 */
function getAdEvent(eventType) {
	let option = tt.getStorageSync('options');
	let shareUserId = option.shareUserId;
	let mId = option.mId;
	let adId = tt.getStorageSync('ad_id');
	let openId = tt.getStorageSync('openId');
	console.log("ad: ", eventType, openId);
	if (!shareUserId && !mId) {
		console.log("no shareUserId and mId");
		return;
	}
	let goodsId = tt.getStorageSync('g_id');
	if (!goodsId) {
		goodsId = "0";
	}
	let temdate = {
		s_u_i: shareUserId,
		m_i: mId,
		a_i: globalData.appId,
		u_i: tt.getStorageSync('openId'),
		e_n: eventType,
		e_t: Date.parse(new Date()),
		g_i: goodsId,
		ad_id: adId,
	};
	console.log('1111111111111111111111111', temdate);
	tt.request({
		url: 'https://es.tuiwa.com/dy_mini/rpt_ent',
		// url: 'http://localhost:9098/dy_mini/rpt_ent',
		header: {
			"content-type": "application/json"
		},
		method: 'POST',
		data: {
			s_u_i: shareUserId,
			m_i: mId,
			a_i: globalData.appId,
			u_i: tt.getStorageSync('openId'),
			e_n: eventType,
			e_t: Date.parse(new Date()),
			g_i: goodsId,
			ad_id: adId,
		},
		success: function(res) {
			console.log(eventType);
			if (eventType == 'getAd') {
				tt.setStorageSync('ad_id',res.data.data);
			}
		},
		fail: function(t) {
			console.log("fail==", JSON.stringify(t));
		}
	});
}

function getAnonymousAdEvent(eventType) { 
	console.log('1getAnonymousAdEvent11');
	let option = tt.getStorageSync('options');
	let shareUserId = option.shareUserId;
	let mId = option.mId;
	console.log("ad: ", eventType);
	// let adId = tt.getStorageSync('ad_id');
	let openId = tt.getStorageSync('openId');
	if (!shareUserId && !mId) {
		console.log("no shareUserId and mId");
		return;
	}
	let goodsId = tt.getStorageSync('g_id');
	if (!goodsId) {
		goodsId = "0";
	}
	var uId = tt.getStorageSync('anonymousCode');
	tt.request({
		url: 'https://vs.tuiwa.com/dy_mini/dy_anonymous_login',
		// url: 'http://192.168.1.222:9099/dy_mini/dy_anonymous_login',
		header: {
			"content-type": "application/json"
		},
		method: 'POST',
		data: {
			s_u_i: shareUserId,
			m_i: mId,
			a_i: globalData.appId,
			u_i: uId,
			e_n: eventType,
			e_t: Date.parse(new Date()),
			g_i: goodsId,
			ad_id: "",
		},
		success: function(res) {
			console.log("88res===",res);
			if (eventType == 'getAd') {
				tt.setStorageSync('ad_id', res.data.data.ad_id);
				tt.setStorageSync('openId', res.data.data.openid);
			}
			
			tt.setStorageSync('goods_id' + res.data.data.goods_id, res.data.data.remain_video_end);
			var remain_video_end = tt.getStorageSync('goods_id' + res.data.data.goods_id);
			console.log(remain_video_end, res.data.data.goods_id);
		},
		fail: function(err) {
			console.log("fail==", JSON.stringify(err));
		}
	});
}
/**
 * 登陆 或者绑定微信
 */

function bindDyWx() {
	console.log('bindDyWx- start');
	return new Promise((resolve, reject) => {
		let option = tt.getStorageSync('options');
		console.log('options==', option);
		if (!option.shareUserId) {
			option.shareUserId = 0;
		}
		if (!option.mId) {
			option.mId = 0;
		}
		if(tt.getStorageSync("shareFlagId")){
			console.log('tt.getStorageSync("shareFlagId")==', tt.getStorageSync("shareFlagId"));
		}else{
			console.log('tt.====');
		}
		// tt.setStorageSync("shareFlagId", 20);
		var goodsId = tt.getStorageSync('g_id');
		if (!goodsId) {
			goodsId = "0";
		}
		if ((option.bindFlag == 1 && option.wxId) || tt.getStorageSync("shareFlagId")) {
			tt.login({
				force: true,
				success: loginRes => {
					tt.setStorageSync('access_token', loginRes.code);
					tt.getUserInfo({
						success: res => {
							console.log("res==", res);
							console.log("datadatadatadatadatadata", option.wxId,loginRes.code, globalData.appId, goodsId);
							tt.request({
								url: 'https://vs.tuiwa.com/dy_mini/bind_dy_wx',
								// url: 'http://192.168.1.222:9099/dy_mini/bind_dy_wx',
								header: {
									"content-type": "application/json"
								},
								method: 'POST',
								data: {
									wxId: option.wxId,
									token: loginRes.code,
									avatarUrl: res.userInfo.avatarUrl,
									country: res.userInfo.country,
									nickName: res.userInfo.nickName,
									appId: globalData.appId,
									goodsId: goodsId,
								},
								success: function(res) {
									console.log('86res===', res.data.data);
									var dyUser = res.data.data.dyUser;

									tt.setStorageSync('goods_id' + goodsId, res.data.data.remain_video_end);
									var remain_video_end = tt.getStorageSync('goods_id' + goodsId);
									console.log(remain_video_end, goodsId);

									if(dyUser){
										dyUser.phone = res.data.data.phone;
										tt.setStorageSync('openId', dyUser
											.openid);
										tt.setStorageSync('dyUser', dyUser);
										tt.setStorageSync('shareFlagId', dyUser
											.id);
										getAdEvent('getAd');
									}
									resolve();
								},
								fail: function(err) {
									reject(err);
									console.log("err====", err);
								}
							});
						}
					});
				}
			});

		} else {
			console.log("login===");
			tt.login({
				success: res => {
					tt.setStorageSync('anonymousCode', res.anonymousCode);
					tt.setStorageSync('dyUser', null);
					getAnonymousAdEvent('getAd');
					resolve();
				}
			});
		}
	});
}

var globalData = {
	appId: "ttb2dfc4622de58bd4",
	currentPage: 0,
};

function auth() {
	tt.authorize({
		scope: "scope.writePhotosAlbum",
		success: (res) => {
			// tt.showToast({ title: "相册授权成功" });
			// console.log('授权相册成功')
		},
		fail: (err) => {
			console.log("authorize fail", err);
			tt.showModal({
				title: "授权出错",
				content: err.errMsg,
				showCancel: false,
			});
		},
	});
}
module.exports = {
	getAnonymousAdEvent: getAnonymousAdEvent,
	getAdEvent: getAdEvent,
	bindDyWx: bindDyWx,
	globalData: globalData,
	auth: auth,
};
